package org.afun.csms_android.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import org.afun.csms_android.CourseAddActivity;
import org.afun.csms_android.R;
import org.afun.csms_android.UserAddActivity;
import org.afun.csms_android.adapter.TeacherStudentAdapter;
import org.afun.csms_android.adapter.UserAdapter;
import org.afun.csms_android.domain.Teacher;
import org.afun.csms_android.domain.User;
import org.afun.csms_android.domain.javaBean.TS;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.util.List;

public class TeacherHome extends Fragment {

    private static Context context;
    private static RecyclerView recyclerView;
    private static TextView emptyText;
    private static User user;
    private Button addCourse;
    private static List<TS> tsList;

    public TeacherHome(Context context, User user) {
        TeacherHome.context = context;
        TeacherHome.user = user;
    }

    public static void showMsg() {
        MyTableOperator myTableOperator = new MyTableOperator(context);
        TableOperator tableOperator = myTableOperator.getTableOperator();
//        System.out.println(user);
        Teacher tea = tableOperator.findTeacherByAccount(user.getAccount());
        tsList = tableOperator.showMyStudent(tea);

        if (tsList.size() == 0) {
            emptyText.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            emptyText.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            recyclerView.setLayoutManager(layoutManager);
            TeacherStudentAdapter teacherStudentAdapter = new TeacherStudentAdapter(tsList, context);
            recyclerView.setAdapter(teacherStudentAdapter);
            DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), layoutManager.getOrientation());
            recyclerView.addItemDecoration(dividerItemDecoration);
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_teacher_home, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        emptyText = (TextView) view.findViewById(R.id.empty_text);
        addCourse = (Button) view.findViewById(R.id.addCourseTeacher);
        showMsg();
        addCourse.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // 跳转到添加课程页面
                Intent intent = new Intent(context, CourseAddActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }

    public void onResume() {
        super.onResume();
        showMsg();
    }

    public static void update() {
        showMsg();
    }
}